Crate env_logger[][src]

A simple logger configured via environment variables which writes to stdout or stderr, for use with the logging facade exposed by the log crate.

Example

#[macro_use] extern crate log;
extern crate env_logger;

use log::Level;

fn main() {
    env_logger::init();

    debug!("this is a debug {}", "message");
    error!("this is printed by default");

    if log_enabled!(Level::Info) {
        let x = 3 * 4; // expensive computation
        info!("the answer was: {}", x);
    }
}

Assumes the binary is main:

$ RUST_LOG=error ./main
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
$ RUST_LOG=info ./main
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
INFO: 2017-11-09T02:12:24Z: main: the answer was: 12
$ RUST_LOG=debug ./main
DEBUG: 2017-11-09T02:12:24Z: main: this is a debug message
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
INFO: 2017-11-09T02:12:24Z: main: the answer was: 12

You can also set the log level on a per module basis:

$ RUST_LOG=main=info ./main
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
INFO: 2017-11-09T02:12:24Z: main: the answer was: 12

And enable all logging:

$ RUST_LOG=main ./main
DEBUG: 2017-11-09T02:12:24Z: main: this is a debug message
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
INFO: 2017-11-09T02:12:24Z: main: the answer was: 12

If the binary name contains hyphens, you will need to replace them with underscores:

$ RUST_LOG=my_app ./my-app
DEBUG: 2017-11-09T02:12:24Z: my_app: this is a debug message
ERROR: 2017-11-09T02:12:24Z: my_app: this is printed by default
INFO: 2017-11-09T02:12:24Z: my_app: the answer was: 12

This is because Rust modules and crates cannot contain hyphens in their name, although cargo continues to accept them.

See the documentation for the log crate for more information about its API.

Enabling logging

Log levels are controlled on a per-module basis, and by default all logging is disabled except for error!. Logging is controlled via the RUST_LOG environment variable. The value of this environment variable is a comma-separated list of logging directives. A logging directive is of the form:

path::to::module=level

The path to the module is rooted in the name of the crate it was compiled for, so if your program is contained in a file hello.rs, for example, to turn on logging for this file you would use a value of RUST_LOG=hello. Furthermore, this path is a prefix-search, so all modules nested in the specified module will also have logging enabled.

The actual level is optional to specify. If omitted, all logging will be enabled. If specified, it must be one of the strings debug, error, info, warn, or trace.

As the log level for a module is optional, the module to enable logging for is also optional. If only a level is provided, then the global log level for all modules is set to this value.

Some examples of valid values of RUST_LOG are:

  • hello turns on all logging for the 'hello' module
  • info turns on all info logging
  • hello=debug turns on debug logging for 'hello'
  • hello,std::option turns on hello, and std's option logging
  • error,hello=warn turn on global error logging and also warn for hello

Filtering results

A RUST_LOG directive may include a regex filter. The syntax is to append / followed by a regex. Each message is checked against the regex, and is only logged if it matches. Note that the matching is done after formatting the log string but before adding any logging meta-data. There is a single filter for all modules.

Some examples:

  • hello/foo turns on all logging for the 'hello' module where the log message includes 'foo'.
  • info/f.o turns on all info logging where the log message includes 'foo', 'f1o', 'fao', etc.
  • hello=debug/foo*foo turns on debug logging for 'hello' where the log message includes 'foofoo' or 'fofoo' or 'fooooooofoo', etc.
  • error,hello=warn/[0-9]scopes turn on global error logging and also warn for hello. In both cases the log message must include a single digit number followed by 'scopes'.

Disabling colors

Colors and other styles can be configured with the RUST_LOG_STYLE environment variable. It accepts the following values:

  • auto (default) will attempt to print style characters, but don't force the issue. If the console isn't available on Windows, or if TERM=dumb, for example, then don't print colors.
  • always will always print style characters even if they aren't supported by the terminal. This includes emitting ANSI colors on Windows if the console API is unavailable.
  • never will never print style characters.

Tweaking the default format

Parts of the default format can be excluded from the log output using the Builder. The following example excludes the timestamp from the log output:

#[macro_use] extern crate log;
extern crate env_logger;

use log::Level;

fn main() {
    env_logger::Builder::from_default_env()
        .default_format_timestamp(false)
        .init();

    debug!("this is a debug {}", "message");
    error!("this is printed by default");

    if log_enabled!(Level::Info) {
        let x = 3 * 4; // expensive computation
        info!("the answer was: {}", x);
    }
}

Specifying defaults for environment variables

env_logger can read configuration from environment variables. If these variables aren't present, the default value to use can be tweaked with the Env type. The following example defaults to log warn and above if the RUST_LOG environment variable isn't set:

#[macro_use] extern crate log;
extern crate env_logger;

use log::Level;

fn main() {
    let env = env_logger::Env::default()
        .filter_or(env_logger::DEFAULT_FILTER_ENV, "warn");
 
    env_logger::Builder::from_env(env).init();
}

Re-exports

pub use self::fmt::Target;
pub use self::fmt::WriteStyle;
pub use self::fmt::Color;
pub use self::fmt::Formatter;

Modules

filter

Filtering for log records.

fmt

Formatting for log records.

Structs

Builder

Builder acts as builder for initializing a Logger.

Env

Set of environment variables to configure from.

Logger

The env logger.

Constants

DEFAULT_FILTER_ENV

The default name for the environment variable to read filters from.

DEFAULT_WRITE_STYLE_ENV

The default name for the environment variable to read style preferences from.

Functions

init

Initializes the global logger with an env logger.

init_from_env

Initializes the global logger with an env logger from the given environment variables.

try_init

Attempts to initialize the global logger with an env logger.

try_init_from_env

Attempts to initialize the global logger with an env logger from the given environment variables.